package com.tsn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsn.common.Result;
import com.tsn.entity.Role;
import com.tsn.entity.User;
import com.tsn.mapper.RoleMapper;
import com.tsn.mapper.UserMapper;
import com.tsn.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tsn
 * @since 2021-07-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Override
    public User getUserInfo(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name",userName);
        User user = userMapper.selectOne(userQueryWrapper);
        System.out.println(user);
        Set<Role> rolesByUserId = roleMapper.findRolesByUserId(user.getId());
        user.setRoles(rolesByUserId);

        return user;
    }

    @Override
    public Result listUserInfos() {
        return Result.success(userMapper.selectUserInfos());
    }
}
